using System;
using System.Text;

using System.IO;
using System.Net;
using System.Web;

using Feathertail.Bots.ProxyBot;
using libsecondlife;

namespace Feathertail.Bots.ProxyBot.Handlers
{
    public class ReverseStringViaWebSite : ProxyHandler
    {
        public ReverseStringViaWebSite(SecondLife client)
            : base(client)
        {
        }

        public override bool HandleIM(libsecondlife.InstantMessage im, libsecondlife.Simulator simulator)
        {
            try
            {
                string WebSite = "http://michaelcortez.com/ProxyBot.php";
                string PostString = "msg=" + im.Message;

                // prepare the web page we will be asking for
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(WebSite);

                // setup post data
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = PostString.Length;

                // write post data into request object
                StreamWriter stOut = new StreamWriter(request.GetRequestStream(), System.Text.Encoding.ASCII);
                stOut.Write(PostString);
                stOut.Close();


                // execute the request, and restrieve the response
                StreamReader responseReader = new StreamReader(request.GetResponse().GetResponseStream());

                string responseString = responseReader.ReadToEnd();

                Console.WriteLine(PostString + " :: " + responseString);
                _Client.Self.InstantMessage(im.FromAgentID, responseString);
            }
            catch (Exception)
            {
                return false;
            }

            return true;
        }
    }
}
